#include "bsp_key.h"

unsigned char Key16_Read(void){

	unsigned int Key_New;
	unsigned char Key_val;
	
	P44 = 0; P42 = 1; P35 = 1; P34 = 1;
	Key_New = (P3&0x0f);
	
	P44 = 1; P42 = 0; P35 = 1; P34 = 1;
	Key_New = (Key_New<<4) | (P3&0x0f);
	
	P44 = 1; P42 = 1; P35 = 0; P34 = 1;
	Key_New = (Key_New<<4) | (P3&0x0f);
	
	P44 = 1; P42 = 1; P35 = 1; P34 = 0;
	Key_New = (Key_New<<4) | (P3&0x0f);
	
	switch(~Key_New){
		
		case 0x8000: Key_val =  4; break;
		case 0x4000: Key_val =  5; break;
		case 0x2000: Key_val =  6; break;	
		case 0x1000: Key_val =  7; break;
		
		case 0x0800: Key_val =  8; break;
		case 0x0400: Key_val =  9; break;
		case 0x0200: Key_val = 10; break;	
		case 0x0100: Key_val = 11; break;
		
		case 0x0080: Key_val = 12; break;
		case 0x0040: Key_val = 13; break;
		case 0x0020: Key_val = 14; break;	
		case 0x0010: Key_val = 15; break;
		
		case 0x0008: Key_val = 16; break;
		case 0x0004: Key_val = 17; break;
		case 0x0002: Key_val = 18; break;	
		case 0x0001: Key_val = 19; break;
		default:Key_val = 0;
	
	}
	return Key_val;
}



unsigned char Key4_Read(void){

	unsigned char Key_New, Key_val;
	P3 |= 0x0f;
	Key_New = P3&0x0f;
	
	switch(~Key_New){
		
		case 0xf8: Key_val = 4; break;
		case 0xf4: Key_val = 5; break;
		case 0xf2: Key_val = 6; break;
		case 0xf1: Key_val = 7; break;
		default:Key_val = 0;
	}
	return Key_val;
}

